<template>
  <a-card :style="multipage?'margin-top:-24px;':''" style="padding-top:20px;" :bordered="false" class="card-area">
    <div v-if="!isSetproject">
      <div :class="advanced ? 'search' : null">
        <!-- 搜索区域 -->
        <a-form layout="horizontal">
          <div :class="advanced ? null: 'fold'">
            <a-row >
              <a-col :md="12" :sm="24" >
                <a-form-item
                  label="项目名称"
                  :labelCol="{span: 5}"
                  :wrapperCol="{span: 18, offset: 1}">
                  <a-input placeholder="请输入项目名称" v-model="queryParams.projectName"/>
                </a-form-item>
              </a-col>
              <!-- <a-col :md="12" :sm="24" >
                <a-form-item
                  label="创建时间"
                  :labelCol="{span: 5}"
                  :wrapperCol="{span: 18, offset: 1}">
                  <range-date @change="handleDateChange" ref="createTime"></range-date>
                </a-form-item>
              </a-col> -->
            </a-row>
          </div>
          <span style="float: right; margin-top: 3px;">
            <a-button type="primary" @click="search">查询</a-button>
            <a-button style="margin-left: 8px" @click="reset">重置</a-button>
          </span>
        </a-form>
      </div>
      <div>
        <div class="operator">
          <a-button v-hasPermission="'role:add'" ghost type="primary" @click="add">新增</a-button>
        </div>
        <!-- 表格区域 -->
        <a-table ref="TableInfo"
                :columns="columns"
                :dataSource="dataSource"
                :pagination="pagination"
                :loading="loading"
                :scroll="{ x: 900 }"
                @change="handleTableChange">
          <template slot="remark" slot-scope="text, record">
            <a-popover placement="topLeft">
              <template slot="content">
                <div style="max-width: 200px">{{text}}</div>
              </template>
              <p style="width: 200px;margin-bottom: 0">{{text}}</p>
            </a-popover>
          </template>
          <template slot="operation" slot-scope="text, record">
            <a-icon v-hasPermission="'role:update'" type="setting" theme="twoTone" twoToneColor="#4a9ff5" @click="edit(record)" title="项目设置"></a-icon>
            &nbsp;
            <a-icon type="right-circle" theme="twoTone" twoToneColor="#1890ff" title="进入实验编辑器"></a-icon>
          </template>
        </a-table>
      </div>
    </div>
    <!-- 设置项目 -->
    <project-edit
      v-else
      ref="roleEdit"
      :projectInfoData="projectInfo.data"
      @back="isSetproject=false"
      @close="handleprojectEditClose"
      @init="fetch"
      @success="handleprojectEditSuccess">
    </project-edit>
    <!-- 新增项目 -->
    <project-add
      @close="handleprojectAddClose"
      @success="handleprojectAddSuccess"
      :roleAddVisiable="roleAdd.visiable">
    </project-add>
  </a-card>
</template>

<script>
import RangeDate from '@/components/datetime/RangeDate'
import ProjectAdd from './ProjectAdd'
import ProjectEdit from './ProjectEdit'
import { mapState } from 'vuex'

export default {
  name: 'Project',
  components: {RangeDate, ProjectAdd, ProjectEdit},
  data () {
    return {
      isSetproject: false,
      advanced: false,
      projectInfo: {
        visiable: false,
        data: {}
      },
      roleAdd: {
        visiable: false
      },
      roleEdit: {
        visiable: false
      },
      queryParams: {
        createTimeFrom: '',
        createTimeTo: '',
        projectName: ''
      },
      dataSource: [],
      paginationInfo: null,
      selectedRowKeys: [],
      pagination: {
        pageSizeOptions: ['10', '20', '30', '40', '100'],
        defaultCurrent: 1,
        defaultPageSize: 10,
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
      },
      loading: false
    }
  },
  computed: {
    ...mapState({
      multipage: state => state.setting.multipage
    }),
    columns () {
      return [{
        title: '项目名称',
        dataIndex: 'projectName'
      }, {
        title: '项目代码',
        dataIndex: 'projectCode',
        scopedSlots: { customRender: 'projectCode' },
        width: 350
      }, {
        title: '项目管理员',
        dataIndex: 'createOper'
      }, {
        title: '创建时间',
        dataIndex: 'createTime'
      }, {
        title: '操作',
        dataIndex: 'operation',
        scopedSlots: { customRender: 'operation' }
      }]
    }
  },
  mounted () {
    this.fetch()
  },
  methods: {
    add () {
      this.roleAdd.visiable = true
    },
    handleprojectAddClose () {
      this.roleAdd.visiable = false
    },
    handleprojectAddSuccess () {
      this.roleAdd.visiable = false
      this.$message.success('新增角色成功')
      this.search()
    },
    view (record) {
      this.projectInfo.data = record
      this.projectInfo.visiable = true
    },
    edit (record) {
      this.projectInfo.data = record
      // this.roleEdit.visiable = true
      this.isSetproject = true
    },
    handleprojectEditSuccess () {
      this.roleEdit.visiable = false
      this.$message.success('新增项目成功')
      this.search()
    },
    handleprojectEditClose () {
      this.roleEdit.visiable = false
    },
    handleDateChange (value) {
      if (value) {
        this.queryParams.createTimeFrom = value[0]
        this.queryParams.createTimeTo = value[1]
      }
    },
    search () {
      this.fetch({
        ...this.queryParams
      })
    },
    reset () {
      // 重置分页
      const { defaultCurrent, defaultPageSize } = this.pagination
      this.$refs.TableInfo.pagination.current = defaultCurrent
      if (this.paginationInfo) {
        this.paginationInfo.current = defaultCurrent
        this.paginationInfo.pageSize = defaultPageSize
      }
      // 重置查询参数
      this.queryParams = {}
      // 清空时间选择
      // this.$refs.createTime.reset()
      this.fetch()
    },
    handleTableChange (pagination, filters, sorter) {
      // 将这两个参数赋值给Vue data，用于后续使用
      this.paginationInfo = pagination
      this.fetch({
        ...this.queryParams
      })
    },
    fetch (params = {}) {
      this.loading = true
      if (this.paginationInfo) {
        // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
        const { current, pageSize } = this.paginationInfo
        this.$refs.TableInfo.pagination.current = current
        this.$refs.TableInfo.pagination.pageSize = pageSize
        params.pageSize = pageSize
        params.pageNum = current
      } else {
        // 如果分页信息为空，则设置为默认值
        const { defaultPageSize, defaultCurrent } = this.pagination
        params.pageSize = defaultPageSize
        params.pageNum = defaultCurrent
      }
      params.keyword = this.queryParams.projectName || ''
      this.$post('project/list', {
        ...params
      }, true).then(r => {
        let data = r.data.data
        let pagination = { ...this.pagination }
        pagination.total = data.total
        this.dataSource = data.rows
        this.pagination = pagination
        this.loading = false
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>

<style lang="less" scoped>
@import "../../../static/less/Common";
</style>
